package com.sf.osms.dep.processor.handler.convert;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.sf.osms.dep.domain.Agent;
import com.sf.osms.dep.domain.Router;
import com.sf.osms.dep.processor.exception.RouterException;
import com.sf.osms.dep.processor.model.EventModel;
/**
 * 转换器链
 * @author sfhq1405
 *
 */
public class ConverterChain implements Converter{
	
	/**
	 * 转换器的集合
	 */
	private List<Converter> conveters = new ArrayList<Converter>();
	
	/**
	 * 转换器执行的指针
	 */
	private ThreadLocal<Integer> indexLocal = new ThreadLocal<Integer>();
	
	public void addConverter(Converter converter){
		this.conveters.add(converter);
	}
	
	/**
	 * 进行路由信息的转化
	 */
	@Override
	public void convert(Router router, Map<String, String> routerFieldAndStr,Agent agentInfo,EventModel eventModel,
			ConverterChain chain) throws RouterException{
		Integer index = indexLocal.get();
		if(index == conveters.size()){
			this.indexLocal.remove();
			return;
		}
		Converter converter = conveters.get(index);
		indexLocal.set(index+1);
		converter.convert(router, routerFieldAndStr,agentInfo,eventModel,chain);
	}
	
	/**
	 * 初始化整个责任链条
	 */
	public void setIntial(){
		this.indexLocal.set(0);
	}
}
